package com.sky.robot.commons.utils;


import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class StringUtil {
    public static String caseFirstLetter(String field, Boolean upperCaseFirstLetter) {

        StringBuilder sb = new StringBuilder();
        String[] fields = field.split("_");

        for (String fieldSplit : fields) {
            if (upperCaseFirstLetter) {
                sb.append(upperCaseFirstLetter(fieldSplit));
            } else {
                sb.append(lowerCaseFirstLetter(fieldSplit));
            }
        }

        return sb.toString();
    }

    /**
     * 给定字符串将首字符转成大写
     */
    public static String upperCaseFirstLetter(String field) {

        if (StringUtils.isBlank(field)) {
            return field;
        }

        return field.substring(0, 1).toUpperCase() + field.substring(1);
    }

    /**
     * 给定字符串将首字符转成小写
     */
    public static String lowerCaseFirstLetter(String field) {

        if (StringUtils.isBlank(field)) {
            return field;
        }

        return field.substring(0, 1).toLowerCase() + field.substring(1);
    }


    public static String[] split(String value) {
        if (!StringUtils.isNotBlank(value)) {
            return new String[]{};
        }
        List<String> valueList = new ArrayList<>();
        String[] values;
        if (value.contains(",")) {
            values = value.split(",");
        } else if (value.contains("\n")) {
            values = value.split("\n");
        } else {
            values = value.split(" ");
        }
        for (String str : values) {
            str = str.trim();
            if (StringUtils.isNotBlank(str)) {
                valueList.add(str);
            }
        }
        String[] result = new String[valueList.size()];
        return valueList.toArray(result);
    }
}
